package com.mlz.mlzmall.ui.activity.act.personal.userLogin;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;

import com.mlz.mlzmall.ui.activity.act.personal.NetworkRequest.MyCenterUtils;
import com.mlz.mlzmall.ui.utils.mlz.Constans;
import com.sina.weibo.sdk.auth.Oauth2AccessToken;
import com.sina.weibo.sdk.auth.WeiboAuthListener;
import com.sina.weibo.sdk.exception.WeiboException;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

/**
 * @description Created by 安帅 on 2016/5/31 18:04.
 * PackageName com.mlz.mlzmall.ui.activity.act.personal.userLogin.
 * ProjectName mlzmall_android.
 */
public class MyAuthListener implements WeiboAuthListener {
    private Context context;
    private Oauth2AccessToken mAccessToken;
    private Handler handler;
    private String type;
    public MyAuthListener(Context context, Handler handler,String type){
        this.context = context;
        this.handler = handler;
        this.type = type;
    }
    @Override
    public void onComplete(Bundle bundle) {
        String token = bundle.getString("access_token");
        String expires_in = bundle.getString("expires_in");
        String uid = bundle.getString("uid");
        mAccessToken = new Oauth2AccessToken(token, expires_in);


        System.out.println("Login---Bundle:::" + bundle);
        if (mAccessToken.isSessionValid()) {
            AccessTokenKeeper.writeAccessToken(context, mAccessToken);

            final Map map = new HashMap();
            map.put("appkey", Constans.APP_KEY);
            map.put("access_token", token);
            map.put("uid", uid);
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        MyAuthListener.sendGETRequest("https://api.weibo.com/2/users/show.json", handler, map);
                    } catch (Exception e) {
                        Toast.makeText(context,
                                "授权失败", Toast.LENGTH_SHORT).show();
                    }
                }
            }).start();

        }
    }

    @Override
    public void onWeiboException(WeiboException e) {
        Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onCancel() {
        Toast.makeText(context,
                "取消授权", Toast.LENGTH_SHORT).show();

    }

    private static void sendGETRequest (String path,Handler handler,
                                        Map<String, String> params) throws Exception{
        StringBuilder sb = new StringBuilder();
        sb.append(path).append("?");
        if(params!=null &params.size()!=0){
            for (Map.Entry<String, String> entry : params.entrySet()) {
                sb.append(entry.getKey()).append("=").append(URLEncoder.encode(entry.getValue(), "utf-8"));
                sb.append("&");
            }
            sb.deleteCharAt(sb.length()-1);
        }
        URL url = new URL(sb.toString());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5000);
        conn.setRequestMethod("GET");
        if(conn.getResponseCode()==200){
            Message message = handler.obtainMessage();
            InputStream inputStream = conn.getInputStream();
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int len;
            byte[] bytes = new byte[1024];
            while ((len = inputStream.read(bytes))!=-1){
                outputStream.write(bytes, 0, len);
            }
            outputStream.close();
            inputStream.close();
            String S = new String(outputStream.toByteArray());
            message.obj = S;
            handler.sendMessage(message);
        }
    }

    public WeiboShareListen weiboShareListen;
    public void setWeiboShareListen(WeiboShareListen weiboShareListen){
        this.weiboShareListen = weiboShareListen;
    }
    public interface WeiboShareListen{
        void succese();
    }
}
